<?php
namespace Orm\Product;

use Base\Exception\RmException;
use Think\Exception;

class ProductClass extends \Base\BaseOrmObject{
    protected   $id,
				$article_id = 0, // 文章ID
				$name = '', // 商品名称
				$describe = '', // 商品描述
				$unit_price = 0, // 商品单价
				$sort = 0, // 商品排序
                $status = self::STATUS_INVALID, // 状态0，预生成无效1，有效
				$is_del = 0, // 默认0  是否删除 1是 0否
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间
    /*
     * 常量
     */
    const PRODUCT_ID_NULL = -1; //商品ID不能为空
    const STATUS_INVALID = 0;
    const STATUS_VALID = 1;

        function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }


    public function loadById($id = 0)
    {
        $model = new \Orm\Product\ProductModel();
        $data = $model->loadById($id);
        self::__init($data);
    }
    public function loadValidById($id = 0)
    {
        $model = new \Orm\Product\ProductModel();
        $data = $model->loadValidById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'article_id' => $this->article_id,
			'name' => $this->name,
			'describe' => $this->describe,
			'unit_price' => $this->unit_price,
			'sort' => $this->sort,
			'status' => self::STATUS_INVALID,
        );

        $model = new \Orm\Product\ProductModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'article_id' => $this->article_id,
			'name' => $this->name,
			'describe' => $this->describe,
			'unit_price' => $this->unit_price,
			'status' => $this->status,
			'sort' => $this->sort,
        );

        $model = new \Orm\Product\ProductModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Product\ProductModel();
        $model->update($data, "`id` = {$id}");
    }

    /**
     * 补全商品图片
     */
    public function completeImageList()
    {
        $this->image_list = array();
        try {
            $product_img_list = new \Orm\ProductImg\ProductImgList();
            $product_img_list['limit'] = \Util\Config::get('max_limit');
            $product_img_list['where'] = "product_id = {$this->id}";
            $product_img_list->load();
            foreach ($product_img_list['lists'] as $item) {
                $this->image_list[] = $this->objToArray($item);
            }
        } catch (Exception $e){}
    }

    public function updateImage($image_id_list)
    {
        try{
            // !,解除当前产品下关联的所用图片
            $product_img_list = new \Orm\ProductImg\ProductImgList();
            $product_img_list['limit'] = \Util\Config::get('max_limit');
            $product_img_list['where'] = "product_id = {$this->id}";
            $product_img_list->load();
            foreach ($product_img_list['lists'] as $item) {
                $item->delete($item['id']);
            }
            // 2,绑定传入的图片列表
            foreach ($image_id_list as $img_id) {
                $img_cls = new \Orm\ProductImg\ProductImgClass();
                $img_cls->loadByIdWithDel($img_id);
                $img_cls['product_id'] = $this->id;
                $img_cls['is_del'] = 0;
                $img_cls->update();
                unset($img_cls);
            }
        } catch (Exception $e){}
    }

    private function checkNecessaryParam()
    {
    }
}